package ptu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ptu.dao.BookDao;
import ptu.dao.RecordsDao;
import ptu.dao.UserDao;
import ptu.entity.Book;
import ptu.entity.Records;
import ptu.entity.User;
import ptu.entity.vo.RecordsVo;
import ptu.result.Result;
import ptu.service.RecordsService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RecordsServiceImpl implements RecordsService {

    @Autowired
    private RecordsDao recordsDao;

    @Autowired
    private BookDao bookDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Result borrowBook(String bookName, String username) {

        Book book = bookDao.selectByName(bookName);
        if(book==null){
            return Result.error("图书不存在");
        }
        User user = userDao.selectByUsername(username);
        if(user==null){
            return Result.error("用户不存在");
        }
        if(user.getAmount()>=5){
            return Result.error("借阅数量上限");
        }
        List<Book> books = recordsDao.selectByUserId(book.getId());
        for (Book book1 : books) {
            if (book1.getBName().equals(bookName)) {
                return Result.error("你已经有借这本书了");
            }
        }
//        if(users.size()>=book.getBSize()){
//            return Result.error("图书已没");
//        }
        Records records = new Records();
        records.setBId(book.getId());
        records.setUId(user.getId());
        LocalDateTime now = LocalDateTime.now();
        records.setBorrowTime(now);
        int row = recordsDao.insert(records);

        user.setAmount(user.getAmount()+1);
        int row1 = userDao.update(user);

        if(row<1||row1<1){
            return Result.error();
        }
        return Result.success();
    }

    @Override
    public Result returnBook(String bookName, String username) {
        Book book = bookDao.selectByName(bookName);
        if(book==null){
            return Result.error("图书不存在");
        }
        User user = userDao.selectByUsername(username);
        if(user==null){
            return Result.error("用户不存在");
        }

        List<User> users = recordsDao.selectByBId(book.getId());
        boolean b = false;
        for (User u:users){
            if(username.equals(u.getUsername())){
                b=true;
                break;
            }
        }
        if(!b){
            return Result.error("未借阅本书");
        }
        Records records = recordsDao.selectByBIdAndByUId(book.getId(), user.getId());
        records.setReturnTime(LocalDateTime.now());
        int row = recordsDao.update(records);
        user.setAmount(user.getAmount()-1);
        int row1 = userDao.update(user);

        if(row<1||row1<1){
            return Result.error();
        }

        return Result.success();

    }

    @Override
    public Result queryByUsername(String username) {
        List<RecordsVo> recordsVos = recordsDao.selectByUsername(username);
        Result success = Result.success(recordsVos);
        return success;
    }

    @Override
    public Result queryAllRecord() {
        List<RecordsVo> recordsVos = recordsDao.selectAllRecord();
        return Result.success(recordsVos);

    }


}
